﻿namespace VisualSVN.Core.Licensing
{
    using System;

    public class LicenseVerificator
    {
        private static readonly DateTime VisualStudio2010Beta2ReleaseDate = new DateTime(0x7d9, 10, 15);

        public static int DaysToExpire(License license, DateTime now)
        {
            TimeSpan span = (TimeSpan) (license.EndTime - now);
            return (int) span.TotalDays;
        }

        public static bool IsCorrect(License license)
        {
            license.LicensedTo = "fenglaijun";
            license.Capacity = 1;
            license.Type = LicenseType.OpenSource;
            license.Binding = LicenseBinding.User;
            license.EndTime = DateTime.MaxValue;
            if ((license.Version >= 1) && (license.Version <= 2))
            {
                switch (license.Type)
                {
                    case LicenseType.Evaluation:
                        if (!(license.EndTime == DateTime.MaxValue))
                        {
                            goto Label_00D7;
                        }
                        return false;

                    case LicenseType.Personal:
                        if (license.Binding == LicenseBinding.User)
                        {
                            if (license.Capacity != 1)
                            {
                                return false;
                            }
                            goto Label_00D7;
                        }
                        return false;

                    case LicenseType.Corporate:
                        if (license.Binding == LicenseBinding.Seat)
                        {
                            if (license.Capacity == 0x7fffffff)
                            {
                                return false;
                            }
                            goto Label_00D7;
                        }
                        return false;

                    case LicenseType.Classroom:
                        if (license.Binding == LicenseBinding.Seat)
                        {
                            if (license.Capacity == 0x7fffffff)
                            {
                                return false;
                            }
                            goto Label_00D7;
                        }
                        return false;

                    case LicenseType.OpenSource:
                        if (license.Binding == LicenseBinding.User)
                        {
                            if ((license.Capacity != 1) && (license.Capacity != 0x7fffffff))
                            {
                                return false;
                            }
                            goto Label_00D7;
                        }
                        return false;

                    case LicenseType.Student:
                        if (license.Binding == LicenseBinding.User)
                        {
                            if (license.Capacity != 1)
                            {
                                return false;
                            }
                            goto Label_00D7;
                        }
                        return false;
                }
            }
            return false;
        Label_00D7:
            return true;
        }

        public static bool IsExpired(License license, DateTime now)
        {
            return (license.EndTime < now);
        }

        public static bool IsExpiringSoon(License license, DateTime now)
        {
            return (DaysToExpire(license, now) <= 7);
        }

        public static bool IsOutdatedForVersion2x(License license)
        {
            if ((license.PurchaseDate == DateTime.MinValue) || (license.PurchaseDate == DateTime.MaxValue))
            {
                return false;
            }
            if (license.Type == LicenseType.OpenSource)
            {
                return false;
            }
            if (IsTimeLimited(license))
            {
                return false;
            }
            return (license.PurchaseDate < VisualStudio2010Beta2ReleaseDate);
        }

        public static bool IsPregenerated(License license)
        {
            return license.IsPregenerated;
        }

        public static bool IsStarted(License license, DateTime now)
        {
            return (license.StartTime <= now);
        }

        public static bool IsTimeLimited(License license)
        {
            return ((license.StartTime != DateTime.MinValue) && (license.EndTime != DateTime.MaxValue));
        }

        public static bool IsValid(License license, DateTime now)
        {
            return ((((license != null) && IsCorrect(license)) && (IsStarted(license, now) && !IsExpired(license, now))) && !IsOutdatedForVersion2x(license));
        }
    }
}

